package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.SetmealDto;
import com.itheima.entry.PageResult;
import com.itheima.entry.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealGroup;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import com.itheima.service.SetmealGroupService;
import com.itheima.service.SetmealService;
import com.itheima.entry.SysConst;
import com.itheima.utils.AliyunUtil;
import com.itheima.utils.RedisUtil;
import com.itheima.vo.CheckGroupVo;
import com.itheima.vo.SetmealVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author jshstart
 * @creat 2020-11-13 17:43
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Reference
    private SetmealGroupService setmealGroupService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Reference
    private CheckGroupService checkGroupService;
    @Reference
    private CheckItemService checkItemService;

    @Override
    public PageResult findAllByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString()!=null){
            queryWrapper.and(wraper->{
                wraper.like("code",queryPageBean.getQueryString()).or()
                .like("name",queryPageBean.getQueryString());
            });

        }
        Page<Setmeal> page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void addAndUpdate(SetmealDto setmealDto) {
        saveOrUpdate(setmealDto.getSetmeal());
        if (setmealDto.getSetmeal().getImg()!=null){
//            redisTemplate.opsForSet().remove("file:set",setmealDto.getSetmeal().getImg());
            RedisUtil.removeSetMember(SysConst.SETMEAL_ALL_SET,setmealDto.getSetmeal().getImg());
        }
        Long setmealId = setmealDto.getSetmeal().getId();
        Long[] groupIds = setmealDto.getGroupIds();
        if (setmealId!=null&&groupIds.length!=0){
            for (Long groupId : groupIds) {
                SetmealGroup setmealGroup = new SetmealGroup();
                setmealGroup.setSetmealId(setmealId);
                setmealGroup.setGroupId(groupId);
                setmealGroupService.save(setmealGroup);
            }
        }
    }

    @Override
    public Long[] findAllIds(Long id) {
        Long[] groupIds=setmealGroupService.findAllIds(id);
        return groupIds;
    }

    @Override
    public void deleteById(Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setIs_delete(1);
        this.updateById(setmeal);
    }

    @Override
    public void clearOssObject() {
        Set<Object> allset = redisTemplate.opsForSet().members("file:allset");
        if (allset!=null){
            for (Object path : allset) {
                String fileKey="file:"+path;
                if (redisTemplate.hasKey(fileKey)==false){
                    AliyunUtil.deleteFile((String) path);
                    redisTemplate.opsForSet().remove("file:allset",path);
                }
            }
        }
    }

//    @Override
//    public List<Setmeal> findAll() {
//        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_delete",0);
//        return list(queryWrapper);
//    }
@Override
public List<Setmeal> findAll() {
    List<Setmeal> list1 = RedisUtil.get("allsetmeals");
    if (list1==null){
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        RedisUtil.set("allsetmeals",list(queryWrapper));
        return list(queryWrapper);
    }else {
        return list1;
    }
}
    @Override
    public SetmealVo findAllGroupItem(Long id) {
        //1.首先获取套餐的基本数据
      SetmealVo setmealVo= baseMapper.findAllSetmeal(id);
        //2.根据套餐id获取检查组的数据
       List<CheckGroupVo> checkGroups= checkGroupService.findAllGroupBySetmealId(id);
        setmealVo.setCheckGroups(checkGroups);
        System.out.println(setmealVo);
        //3.根据检查组的id获取检查项的数据
        if (checkGroups!=null){
            for (CheckGroupVo checkGroup : checkGroups) {
                checkGroup.setCheckItems(checkItemService.findAllItemByGroupId(checkGroup.getId()));
            }
        }
        System.out.println(setmealVo);
        return setmealVo;
    }

    @Override
    public Setmeal findAllById(Long id) {
      Setmeal setmeal  =baseMapper.findAllById(id);
        return setmeal;
    }

    /**
     * 查找热门套餐
     * @return
     */
    @Override
    public List<Map> findHotSetmeal() {
        return this.baseMapper.findHotSetmeal();
    }


}

